/* 请求模块 */
import constant from '@/common/constant.js';

let loadingCount = 0;
let loadingTimer;

/**
 * get请求
 * 
 * @param {String}         url    接口地址
 * @param {Object}         param  请求参数
 * @param {Object}         header 请求头
 * @param {boolean|String} isLoad 加载动画开关
 */
async function get(url = '', param = {}, header = {}, isLoad = true) {
	return await this.request('get', url, param, header, isLoad);
}
/**
 * post请求
 * 
 * @param {String}         url    接口地址
 * @param {Object}         param  请求参数
 * @param {Object}         header 请求头
 * @param {boolean|String} isLoad 加载动画开关
 */
async function post(url = '', param = {}, header = {}, isLoad = true) {
	return await this.request('post', url, param, header, isLoad);
}

/**
 * put请求
 * 
 * @param {String}         url    接口地址
 * @param {Object}         param  请求参数
 * @param {Object}         header 请求头
 * @param {boolean|String} isLoad 加载动画开关
 */
async function put(url = '', param = {}, header = {}, isLoad = true) {
	return await this.request('put', url, param, header, isLoad);
}

/**
 * delete请求
 * 
 * @param {String}         url    接口地址
 * @param {Object}         param  请求参数
 * @param {Object}         header 请求头
 * @param {boolean|String} isLoad 加载动画开关
 */
async function del(url = '', param = {}, header = {}, isLoad = true) {
	return await this.request('delete', url, param, header, isLoad);
}

/**
 * 请求数据处理
 * 
 * @param {String}         method 请求方式
 * @param {String}         url    接口地址
 * @param {Object}         param  请求参数
 * @param {Object}         header 请求头
 * @param {boolean|String} isLoad 加载动画开关
 */
async function request(method, url, param, header = {}, isLoad) {
	// 默认数据
	param = param || {}, header = header || {};

	// 处理url
	// url = this.server + url
	url = constant.baseUrl + url
	// url.indexOf('://') === -1 && (url = this.server + url);
	// header.appVersion = '1.0.1';
	// header.auditPlatform = 'huawei';
	// header.device = '351564491019816';
	// header.packageId = 2;
	// header.platform = 'mini_app';
	// header.sign = 'a919851f7547e4b4af3a1fdfa4be53c0';
	// header.uid = ''
	// 处理token
	// let token = uni.getStorageSync('token');
	let userInfo = uni.getStorageSync('userInfo');
	
	if (userInfo.token) {
		header.token = userInfo.token;
	}
	header.packageName = constant.packageName;
	header['content-type'] = 'application/x-www-form-urlencoded';

	// 加载动画
	if (isLoad) {
		if (loadingCount === 0) {
			uni.showLoading({
				title: typeof isLoad == 'string' ? isLoad : '数据加载中'
			});
			loadingTimer = setTimeout(() => {
				if (loadingCount > 0) {
					uni.hideLoading();
					loadingCount = 0;
				}
			}, 10000);
		}
		loadingCount++;
	}
	// 发送请求
	let res = (await uni.request({
		url: url,
		data: param,
		header: header,
		method: method
	}));
	
	console.log("url:" + url);
	// console.log("header:" + JSON.stringify(header));
	// console.log("param:" + JSON.stringify(param));
	
	if (!res || !res[1]) {
		console.log("返回失败："+res);
		return {
			status: 0,
			code: 0,
			msg: '网络连接失败',
			data: res
		};
	}
	// console.log("返回数据:" + JSON.stringify(res[1].data));

	let statusCode = res[1].statusCode;
	res = res[1].data;

	// 关闭动画
	if (isLoad) {
		uni.hideLoading();
		loadingCount = 0;
	}

	// 错误500
	if (statusCode === 500) {
		return {
			status: 0,
			code: statusCode,
			msg: '网络开小差了',
			data: res
		};
	}

	// 错误401
	if (statusCode === 401 || res.status == 401) {
		uni.removeStorageSync('token');
		uni.removeStorageSync('user');
		this.show('未登录', 'none', 1000, '/pages/login/login', 1);
		return {
			status: 0,
			code: statusCode,
			msg: '未登录...',
			data: res
		};
	}

	// 其他错误
	if (statusCode !== 200) {
		return {
			status: 0,
			code: statusCode,
			msg: '网络连接失败',
			data: res
		};
	}

	// 请求成功
	// res.status = res.code == 200 ? 1 : 0;
	return res;

}

module.exports = {
	get,
	post,
	put,
	del,
	request,
}